home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 296_01.zip / SYMTAB.C < prev    next >
C/C++ Source or Header  |  1993-04-01  |  4KB  |  165 lines

  1. /*
  2.  * symtab.c
  3.  *
  4.  * This module contains the functions to init the symbol table and
  5.  * hash, store, find or test a symbol.
  6.  *
  7.  */
  8.  
  9. #include <ctype.h>
  10. #include <stdio.h>
  11. #include "ctocxx.h"
  12.  
  13. #define NIL 0
  14.  
  15. static struct symbol *st[TABSIZ];  /* this is the hashing symbol table */
  16. static char sbuf[SYMSIZ];        /* static area for symbol name storage */
  17.  
  18. int testsym();
  19. char *malloc();
  20. char *strcpy();
  21. char *strcat();
  22.  
  23.  
  24. /*--------------------------------------------------------------*/
  25. /* hash does more than just figure out a hash code for a symbol */
  26. /* name.  It also puts the symbol into a temporary static area  */
  27. /* to assure that there is no garbage in the storage area after */
  28. /* the trailing NULL character.                                 */
  29.  
  30. static hash(symbol_text)
  31.     char symbol_text[];
  32.     {
  33.     register char *ptr;
  34.     register int i;
  35.  
  36.     /* clear local area */
  37.     for (ptr = sbuf; ptr < sbuf + SYMSIZ; ++ptr)
  38.         *ptr = '\0';
  39.  
  40.     /* copy symbol into local area - only SYMSIZ-1 characters are used */
  41.     i = 0;
  42.     ptr = symbol_text;
  43.  
  44.     while (*ptr)
  45.         {
  46.         if (i >= SYMSIZ - 1)
  47.             break;
  48.         sbuf[i++] = *ptr++;
  49.         }
  50.  
  51.     /* figure hash code */
  52.     ptr = sbuf;
  53.     return((50*(ptr[0]) + 10*(ptr[1]) + (ptr[2]) + ptr[5])%TABSIZ);
  54.     }
  55.  
  56. /*------------------------------------------------------------*/
  57. /* newpiece will allocate a new area,  assign the symbol name */
  58. /* currently in sbuf, and initialize the rest of the fields.  */
  59. /* newpiece returns the address of the new area allocated.    */
  60.  
  61. static struct symbol *newpiece(val)
  62.     long val;
  63.     {
  64.     register SYMPTR s;
  65.  
  66.     s = (SYMPTR) (malloc(sizeof (SYM)));
  67.     strcpy(s->name,sbuf);
  68.     s->muldef = FALSE;
  69.     s->type = 0;    /* the default */
  70.     s->chain  = NIL;
  71.     s->uval.lval = val;
  72.     return(s);
  73.     }
  74. /*------------------------------------------------------------*/
  75.  
  76. initsymtab()
  77.     {
  78.     int i;
  79.  
  80.     for (i=0; i<TABSIZ ; i++)
  81.         st[i]=NIL;
  82.     }
  83.  
  84. /*--------------------------------------------------------------------*/
  85. /* storesym - stores new symbol name and returns ptr to symbol struct */
  86.  
  87. SYMPTR storesym(x,val)
  88.     char x[];   /* symbol name */
  89.     long val;   /* symbols value */
  90.     {
  91.     int found;
  92.     int h;
  93.     register SYMPTR s;
  94.  
  95.     h=hash(x);
  96.     if (st[h] == NIL )
  97.         {
  98.         st[h] = newpiece(val);
  99.         }
  100.     else
  101.         {
  102.         /* slot not empty, check collision chain for multiply defined */
  103.         found = FALSE;
  104.         for (s = st[h] ; s != NIL ; s = s->chain)
  105.             {
  106.             if (testsym(s->name))
  107.                 {
  108.                 /* Aha!  it IS on this chain */
  109.                 found = TRUE;
  110.                 break;
  111.                 }
  112.             }
  113.         if (found)
  114.             {
  115.             /* found old symbol definition */
  116.             s->muldef=TRUE;
  117.             return(s);
  118.             }
  119.  
  120.         /*link new sym onto collision chain */
  121.         s = st[h];
  122.         st[h] = newpiece(val);
  123.         st[h]->chain = s;
  124.         }
  125.  
  126.     /* a new piece was allocated onto the beginning of st[h] */
  127.     return(st[h]);
  128.     }
  129.  
  130. /*------------------------------------------------------------*/
  131. /* findsym - looks up name in symbol table and returns ptr to struct */
  132.  
  133. SYMPTR findsym(tag)
  134. char *tag;
  135.     {
  136.     register SYMPTR p;
  137.     int h;
  138.  
  139.     h = hash(tag);
  140.     p = st[h];
  141.     while ( p!= NIL )
  142.         {
  143.         if (testsym(p->name))
  144.             break;              /* match found */
  145.         else
  146.             p=p->chain;
  147.         }
  148.     return(p);
  149.     }
  150.  
  151. /*----------------------------------------------------------*/
  152. /* testsym - checks for name match, used by findsym()   */
  153.  
  154. int testsym(name)
  155.     char name[];
  156.     {
  157.     int i;
  158.     i=0;
  159.     while( sbuf[i]==name[i])
  160.         if( sbuf[++i] == '\0' && name[i] == '\0' )
  161.             return(TRUE);
  162.     return(FALSE);
  163.     }
  164.  
  165.